﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;
using HYFamilyLibDataManager;

namespace YArchitech.HVAC
{
	public partial class LayFamilyForm : System.Windows.Forms.Form
	{
		public LayFamilyForm(TreeView tv, Document doc)
		{
			this.InitializeComponent();
			//base.Icon = Resource.YArchitechIcon;
			this.hvacDR = new HVACDataReader(doc);
			this.treeView = tv;
			base.Controls.Add(this.treeView);
			this.initTreeView();
			this.initFamilyTypeCB();
			this.doc = doc;
		}

		public LayFamilyForm(TreeView tv, Document doc, string EquipmentName)
		{
			this.InitializeComponent();
			//base.Icon = Resource.YArchitechIcon;
			this.hvacDR = new HVACDataReader(doc);
			this.treeView = tv;
			base.Controls.Add(this.treeView);
			this.initTreeView();
			this.initFamilyTypeCB();
			this.doc = doc;
			this.EquipmentCatgeory = EquipmentName;
			this.initFormUI();
		}

		private void initFormUI()
		{
			string equipmentCatgeory = this.EquipmentCatgeory;
			if (equipmentCatgeory == "AirTerminal")
			{
				this.Text = "风口布置-族立得";
				this.btnLay1.Text = "区域布置";
				this.btnLay2.Visible = true;
				this.btnLay2.Text = "自动布置";
				this.btnLay3.Visible = true;
				this.btnLay3.Text = "单个布置";
				this.btnLay3.TabIndex = 0;
				this.label3.Location = new System.Drawing.Point(237, 373);
				this.txtOffSet.Location = new System.Drawing.Point(340, 370);
				this.labelAir.Visible = true;
				this.hytbCount.Visible = true;
				return;
			}
			if (!(equipmentCatgeory == "EquipmentCoil"))
			{
				return;
			}
			this.Text = "风机盘管布置-族立得";
			this.btnLay1.Text = "区域布置";
			this.btnLay2.Visible = true;
			this.btnLay2.Text = "单个布置";
			this.btnLay2.TabIndex = 0;
		}

		private void initFamilyTypeCB()
		{
			TreeNode treeNode = null;
			if (this.treeView.Nodes.Count > 0)
			{
				treeNode = TreeViewTools.GetFirstNode(this.treeView.Nodes[0]);
				this.treeView.SelectedNode = treeNode;
			}
			this.DisPlayImgAndParameter(treeNode);
		}

		private void initTreeView()
		{
			this.treeView.Location = new System.Drawing.Point(14, 16);
			this.treeView.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
			this.treeView.Width = 200;
			this.treeView.Height = 390;
			this.treeView.NodeMouseClick += this.treeView_NodeMouseClick;
		}

		protected void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
		{
			this.DisPlayImgAndParameter(e.Node);
		}

		protected void DisPlayImgAndParameter(TreeNode treeNode)
		{
			try
			{
				if (treeNode != null && treeNode.Tag is DbFamily)
				{
					Image image = (treeNode.Tag as DbFamily).Image;
					this.picBoxpreview.Image = image;
					this.DisPlayFamilyType(treeNode);
					if (this.treeSelectedNode != null)
					{
                        this.treeSelectedNode.BackColor = System.Drawing.Color.Transparent;
                        this.treeSelectedNode.ForeColor = System.Drawing.Color.Black;
					}
					this.treeSelectedNode = treeNode;
                    treeNode.BackColor = System.Drawing.Color.DodgerBlue;
                    treeNode.ForeColor = System.Drawing.Color.White;
					if (this.lsbFamilyType.SelectedItem == null && this.lsbFamilyType.Items.Count > 0)
					{
						this.lsbFamilyType.SelectedItem = this.lsbFamilyType.Items[0];
					}
				}
			}
			catch (Exception)
			{
			}
		}

		private void DisPlayFamilyType(TreeNode treeNode)
		{
			if (treeNode != null && treeNode.Tag is DbFamily)
			{
				this.lsbFamilyType.Items.Clear();
				List<string> symbols = (treeNode.Tag as DbFamily).Symbols;
				IComparer<string> comparer = new ListSortByNumber();
				symbols.Sort(comparer);
				foreach (string item in symbols)
				{
					this.lsbFamilyType.Items.Add(item);
				}
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			try
			{
				if (this.TextBoxIsEmpty(false))
				{
					string equipmentCatgeory = this.EquipmentCatgeory;
					if (!(equipmentCatgeory == "AirTerminal"))
					{
						if (equipmentCatgeory == "EquipmentCoil")
						{
							this.layType = 3;
							LayAirTerminalAreaForm layAirTerminalAreaForm = new LayAirTerminalAreaForm(this.userSetting, "风盘");
							if (layAirTerminalAreaForm.ShowDialog() != DialogResult.OK)
							{
								return;
							}
							this.layAreaParam = layAirTerminalAreaForm.layAreaParameter;
							this.rectLays = layAirTerminalAreaForm.rectLayParas;
						}
					}
					else
					{
						LayAreaFormNew layAreaFormNew = new LayAreaFormNew(this.userSetting);
						if (layAreaFormNew.ShowDialog() != DialogResult.OK)
						{
							return;
						}
						this.InitLayType(layAreaFormNew.connectType);
						if (layAreaFormNew.connectType == "Line")
						{
							this.layLineParam = layAreaFormNew.layLineParameter;
						}
						else if (layAreaFormNew.connectType == "Rect")
						{
							this.layAreaParam = layAreaFormNew.layRectParameter;
							this.rectLays = layAreaFormNew.rectLayParas;
						}
						else if (layAreaFormNew.connectType == "Mid")
						{
							this.layMidParam = layAreaFormNew.layMidParameter;
						}
					}
					if (this.lsbFamilyType.SelectedItem != null && this.treeSelectedNode != null && "" != this.lsbFamilyType.SelectedItem.ToString())
					{
						this.FamilyLoad();
						this.OffSet = double.Parse(this.txtOffSet.Text.Trim());
						if (5 == this.isOnFace)
						{
							YJKMessageBox.Information("基于面设备的批量布置，不支持当前Revit版本");
						}
						else
						{
							base.DialogResult = DialogResult.OK;
						}
					}
					else
					{
						YJKMessageBox.Information("请选择族类型");
					}
				}
			}
			catch (Exception ex)
			{
				YJKMessageBox.Information(ex.Message);
			}
		}

		private void FamilyLoad()
		{
			DbFamily family = this.treeSelectedNode.Tag as DbFamily;
			string typeName = this.lsbFamilyType.SelectedItem.ToString();
			DbFamilySymbol familySymbol = this.hvacDR.GetFamilySymbol(family, typeName);
			if (familySymbol != null)
			{
				this.fs = familySymbol.Create(this.doc, ref this.isOnFace);
				if (this.fs == null)
				{
					throw new Exception("加载族失败");
				}
			}
			else
			{
				MessageBox.Show("读取族库失败", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
			}
		}

		private void btnLay2_Click(object sender, EventArgs e)
		{
			try
			{
				string equipmentCatgeory = this.EquipmentCatgeory;
				if (!(equipmentCatgeory == "AirTerminal"))
				{
					if (equipmentCatgeory == "EquipmentCoil")
					{
						if (this.TextBoxIsEmpty(false))
						{
							this.layType = 0;
							if (this.lsbFamilyType.SelectedItem != null && this.treeSelectedNode != null && "" != this.lsbFamilyType.SelectedItem.ToString())
							{
								this.FamilyLoad();
								this.OffSet = double.Parse(this.txtOffSet.Text.Trim());
								base.DialogResult = DialogResult.OK;
							}
							else
							{
								YJKMessageBox.Information("请选择族类型");
							}
						}
					}
				}
				else if (this.TextBoxIsEmpty(true))
				{
					this.InitLayType("Auto");
					if (this.layAutoParam == null)
					{
						this.layAutoParam = new LayAutoParameter();
					}
					this.layAutoParam.AllCount = int.Parse(this.hytbCount.Text.Trim());
					if (this.lsbFamilyType.SelectedItem != null && this.treeSelectedNode != null && "" != this.lsbFamilyType.SelectedItem.ToString())
					{
						this.FamilyLoad();
						this.OffSet = double.Parse(this.txtOffSet.Text.Trim());
						if (5 == this.isOnFace)
						{
							YJKMessageBox.Information("基于面设备的批量布置，不支持当前Revit版本");
						}
						else
						{
							base.DialogResult = DialogResult.OK;
						}
					}
					else
					{
						YJKMessageBox.Information("请选择族类型");
					}
				}
			}
			catch (Exception ex)
			{
				YJKMessageBox.Information(ex.Message);
			}
		}

		private void btnLay3_Click(object sender, EventArgs e)
		{
			try
			{
				if (this.TextBoxIsEmpty(false))
				{
					string equipmentCatgeory = this.EquipmentCatgeory;
					if (equipmentCatgeory == "AirTerminal")
					{
						this.InitLayType("Point");
					}
					if (this.lsbFamilyType.SelectedItem != null && this.treeSelectedNode != null && "" != this.lsbFamilyType.SelectedItem.ToString())
					{
						this.FamilyLoad();
						this.OffSet = double.Parse(this.txtOffSet.Text.Trim());
						base.DialogResult = DialogResult.OK;
					}
					else
					{
						YJKMessageBox.Information("请选择族类型");
					}
				}
			}
			catch (Exception ex)
			{
				YJKMessageBox.Information(ex.Message);
			}
		}

		private void InitLayType(string layType)
		{
			if (layType == "Point")
			{
				this.layType = 0;
				return;
			}
			if (layType == "Auto")
			{
				this.layType = 5;
				return;
			}
			if (layType == "Line")
			{
				this.layType = 1;
				return;
			}
			if (layType == "Cross")
			{
				this.layType = 2;
				return;
			}
			if (layType == "Rect")
			{
				this.layType = 3;
				return;
			}
			if (!(layType == "Mid"))
			{
				return;
			}
			this.layType = 4;
		}

		private bool TextBoxIsEmpty(bool auto = false)
		{
			if (this.txtOffSet.Text.Trim() == "")
			{
				MessageBox.Show("标高不能为空", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				return false;
			}
			if (this.hytbCount.Visible && auto && this.hytbCount.Text.Trim() == "")
			{
				YJKMessageBox.Information("风口数量不能为空！");
				return false;
			}
			return true;
		}

		public FamilySymbol fs;

		public double OffSet;

		private Document doc;

		private TreeView treeView = new TreeView();

		private HVACDataReader hvacDR;

		private TreeNode treeSelectedNode;

		private string EquipmentCatgeory = string.Empty;

		public int layType;

		public LayAreaParameter layAreaParam;

		public LayLineParameter layLineParam;

		public LayMidParameter layMidParam;

		public LayAutoParameter layAutoParam;

		private HVACUserSetting userSetting = new HVACUserSetting();

		private int isOnFace;

		public RectLayParamers rectLays;
	}
}
